
import { connect } from 'umi';

import { Row, Col } from 'antd';

import cls from 'classnames';

import './styles.less';

import {
  DictionaryContext,
  useDictionaryTypeData,
  useDictionaryTypeReducer,
} from './hooks';

import {
  DictionaryTypeLayout,
  DictionaryTypeSearch,
  DictionaryTypeTitle,
  DictionaryTypeTree,
  DictionaryTableLayout,
  DictionaryTableHeader,
  DictionaryTableContent,
} from './components';

interface IDictionaryTreeController {
  curRes: Record<string, any>
}

const DictionaryTreeController: React.FC<IDictionaryTreeController> = ({
  curRes
}) => {
  const prefixCls = 'dic-';

  const {
    treeData,
    tableLoading,
    tableDataSource,
    treeLoading,
    reloadTree,
    reloadTableDataSource,
  } = useDictionaryTypeData();

  const {
    searchValue,
    selectKeys,
    expandedKeys,
    autoExpandParent,
    searchTableValue,
    expandedRowKeys,
    commonDispatch,
  } = useDictionaryTypeReducer();

  return (
    <DictionaryContext.Provider
      value={{
        curRes,
        treeData,
        prefixCls,
        selectKeys,
        searchValue,
        expandedKeys,
        autoExpandParent,
        searchTableValue,
        expandedRowKeys,
        commonDispatch,
        reloadTableDataSource,
      }}
    >
      <Row className={cls(prefixCls + 'row')} gutter={[8, 8]}>
        <Col span={6} className={cls(prefixCls + 'col')}>
          <DictionaryTypeLayout clsName={'tree-box'}>
            <DictionaryTypeTitle reloadTree={reloadTree} />
            <DictionaryTypeLayout clsName={'tree-body'}>
              <DictionaryTypeSearch reloadTree={reloadTree} />
              <DictionaryTypeTree
                loading={treeLoading}
                reloadTree={reloadTree}
              />
            </DictionaryTypeLayout>
          </DictionaryTypeLayout>
        </Col>
        <Col span={18} className={cls(prefixCls + 'col')}>
          <DictionaryTableLayout clsName={'table-box'}>
            <DictionaryTableHeader dataSource={tableDataSource} />
            <DictionaryTableContent
              loading={tableLoading}
              dataSource={tableDataSource}
            />
          </DictionaryTableLayout>
        </Col>
      </Row>
    </DictionaryContext.Provider>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
}))(DictionaryTreeController);
